package com.example.aspects;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
public class LogAspect {
    @Pointcut("execution(* com.example.controller.*.*.*(..))")
    public void pt() {}

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = pjp.proceed();
        long end = System.currentTimeMillis();

        // 获取方法入参、方法名称、返回值
        // 获取方法本身（method）
        Signature signature = pjp.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        String methodName = method.getName();
        Annotation[] annotations = method.getAnnotations();
        Class<?> returnType = method.getReturnType();
        Class<?>[] parameterTypes = method.getParameterTypes();
        System.out.println(("接口名称：" + methodName + "接口注解：" + annotations + "接口返回值类型：" + returnType + "接口参数类型：" + parameterTypes));

        System.out.println("接口耗时:" +(end-start)+"ms");

        return proceed;
    }
}
